using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ReplyDAL
    {
        public static int Save(ReplyInfo info)
        {
            string sql = string.Format("insert into [reply]([AdminID],[UserID],[OrderID],[OrderDetailID],[ProductID],[IntroScore],[ServerScore],[PostScore],[Format],[Context],[CreateDate]) values(@AdminID,@UserID,@OrderID,@OrderDetailID,@ProductID,@IntroScore,@ServerScore,@PostScore,@Format,@Context,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@OrderID", info.OrderID),
                new SqlParameter("@OrderDetailID", info.OrderDetailID),
                new SqlParameter("@ProductID", info.ProductID),
                new SqlParameter("@IntroScore", info.IntroScore),
                new SqlParameter("@ServerScore", info.ServerScore),
                new SqlParameter("@PostScore", info.PostScore),
                new SqlParameter("@Format", info.Format),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        
        public static ReplyInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[reply]");
            return SimpleData(dr);
        }
        public static List<ReplyInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[reply]");
            return ListSimpleData(dt);
        }

        public static List<ReplyInfo> FindPage(int productid,string keyword, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (productid != -1)
            {
                filter.AppendFormat(" and [reply].ProductID = {0}", productid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [reply].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [reply].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindCPage(startindex, pagesize, order, desc, filter, "[reply_page]", out count);
            return ListAllData(dt);
        }
        public static List<ReplyInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [reply]"));
            return ListSimpleData(dt);
        }
        public static List<ReplyInfo> FindTopByProductID(int adminid, int top , int productid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top {0} reply.*,[user].RealName,[user].HeadImg from [reply] left join [user] on [reply].UserID=[user].ID where [reply].AdminID={1} and [reply].ProductID={2}", top, adminid, productid));
            return ListAllData(dt);
        }
        public static int FindCountByProductID(int adminid, int productid)
        {
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(string.Format("select count(1) from [reply] where AdminID={0} and ProductID={1}", adminid, productid)));
        }
        private static ReplyInfo SimpleData(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.OrderDetailID = DataConvert.ToInt(dr["OrderDetailID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.IntroScore = DataConvert.ToInt(dr["IntroScore"]);
                info.ServerScore = DataConvert.ToInt(dr["ServerScore"]);
                info.PostScore = DataConvert.ToInt(dr["PostScore"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy.MM.dd");
            }
            return info;
        }
        private static ReplyInfo AllData(DataRow dr)
        {
            ReplyInfo info = new ReplyInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.OrderID = DataConvert.ToInt(dr["OrderID"]);
                info.OrderDetailID = DataConvert.ToInt(dr["OrderDetailID"]);
                info.ProductID = DataConvert.ToInt(dr["ProductID"]);
                info.IntroScore = DataConvert.ToInt(dr["IntroScore"]);
                info.ServerScore = DataConvert.ToInt(dr["ServerScore"]);
                info.PostScore = DataConvert.ToInt(dr["PostScore"]);
                info.Format = DataConvert.ToString(dr["Format"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy.MM.dd");

                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.HeadImg = Commons.domain + DataConvert.ToString(dr["HeadImg"]);
            }
            return info;
        }
        private static List<ReplyInfo> ListSimpleData(DataTable dt)
        {
            List<ReplyInfo> list = new List<ReplyInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ReplyInfo> ListAllData(DataTable dt)
        {
            List<ReplyInfo> list = new List<ReplyInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

